package com.open.system.controller;

import java.util.*;

import cn.hutool.json.JSONUtil;
import com.open.common.task.HuaDaoPost;
import com.open.system.domain.LoanUserDO;
import com.open.system.service.LoanAdminLogService;
import com.open.system.service.LoanUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.open.system.domain.LoanHuadaoInfoDO;
import com.open.system.service.LoanHuadaoInfoService;
import com.open.common.utils.PageUtils;
import com.open.common.utils.Query;
import com.open.common.utils.R;

import javax.servlet.http.HttpServletRequest;

import static com.open.common.utils.ShiroUtils.getUser;

/**
 * 华道借贷信息
 *
 * @author aaa
 * @email aaax
 * @date 2018-11-23 10:07:57
 */

@Controller
@RequestMapping("/system/loanHuadaoInfo")
public class LoanHuadaoInfoController {
    @Autowired
    private LoanHuadaoInfoService loanHuadaoInfoService;
    @Autowired
    private LoanUserService userService;
    @Autowired
    private LoanAdminLogService loanAdminLogService;

    @GetMapping()
    @RequiresPermissions("system:loanHuadaoInfo:loanHuadaoInfo")
    String LoanHuadaoInfo() {
        return "system/loanHuadaoInfo/loanHuadaoInfo";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("system:loanHuadaoInfo:loanHuadaoInfo")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<LoanHuadaoInfoDO> loanHuadaoInfoList = loanHuadaoInfoService.list(query);
        int total = loanHuadaoInfoService.count(query);
        PageUtils pageUtils = new PageUtils(loanHuadaoInfoList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("system:loanHuadaoInfo:add")
    String add() {
        return "system/loanHuadaoInfo/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("system:loanHuadaoInfo:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        LoanHuadaoInfoDO loanHuadaoInfo = loanHuadaoInfoService.get(id);
        model.addAttribute("loanHuadaoInfo", loanHuadaoInfo);
        return "system/loanHuadaoInfo/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("system:loanHuadaoInfo:add")
    public R save(LoanHuadaoInfoDO loanHuadaoInfo) {
        if (loanHuadaoInfoService.save(loanHuadaoInfo) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("system:loanHuadaoInfo:edit")
    public R update(LoanHuadaoInfoDO loanHuadaoInfo) {
        loanHuadaoInfoService.update(loanHuadaoInfo);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("system:loanHuadaoInfo:remove")
    public R remove(Long id) {
        if (loanHuadaoInfoService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("system:loanHuadaoInfo:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] ids) {
        loanHuadaoInfoService.batchRemove(ids);
        return R.ok();
    }


    @GetMapping("/huaDaoInfo/{id}")
    @RequiresPermissions("system:loanHuadaoInfo:loanHuadaoInfo")
    String getMobileHuaDaoInfo(@PathVariable("id") Long id, Model model, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userId", id);
        LoanUserDO user = userService.get(id);
        if (user.getRealName() == null || user.getRealName() == "") {
            user.setRealName("未实名");
        }
        //--操作日志--开始--
        String shopId = getUser().getEmail();
        String adminName = getUser().getName();
        String Module = "借贷信息";
        String detail = "查看了【" + user.getRealName() + "】的借贷信息";
        loanAdminLogService.saveLog(shopId, adminName, Module, detail);
        //--操作日志--结束--

        ArrayList<Map> arrayList_EMR002 = new ArrayList<Map>();
        ArrayList<Map> arrayList_EMR004 = new ArrayList<Map>();
        ArrayList<Map> arrayList_EMR007 = new ArrayList<Map>();
        ArrayList<Map> arrayList_EMR009 = new ArrayList<Map>();
        ArrayList<Map> arrayList_EMR012 = new ArrayList<Map>();
        ArrayList<Map> arrayList_EMR013 = new ArrayList<Map>();

        String token = HuaDaoPost.getToken();
        if (token.contains("errcode")) {
            model.addAttribute("arrayList_EMR002", arrayList_EMR002);
            model.addAttribute("arrayList_EMR004", arrayList_EMR004);
            model.addAttribute("arrayList_EMR007", arrayList_EMR007);
            model.addAttribute("arrayList_EMR009", arrayList_EMR009);
            model.addAttribute("arrayList_EMR012", arrayList_EMR012);
            model.addAttribute("arrayList_EMR013", arrayList_EMR013);
            model.addAttribute("CODE", token);
            model.addAttribute("PHONE", user.getMobile());
            model.addAttribute("user", user);
            return "system/loanHuadaoInfo/huaDaoInfo";
        }

        HashMap<String, Object> parmMap = new HashMap<String, Object>();
        parmMap.put("ACCESS_TOKEN", token);
        parmMap.put("Phone", user.getMobile());
        parmMap.put("cycle", "24");
        parmMap.put("Platform", "0");

        String emda_xdzh_dt = HuaDaoPost.getEmda_xdzh_dt(parmMap);
        cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(emda_xdzh_dt);

        LoanHuadaoInfoDO loanHuadaoInfoDO = new LoanHuadaoInfoDO();
        loanHuadaoInfoDO.setJsonData(emda_xdzh_dt);
        loanHuadaoInfoDO.setCreateTime(new Date());
        loanHuadaoInfoDO.setUserId(id);
        loanHuadaoInfoDO.setRealName(user.getRealName());
        loanHuadaoInfoDO.setRemark1(token);

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", id);
        List<LoanHuadaoInfoDO> list = loanHuadaoInfoService.list(params);
        if (list.size() > 0) {
            loanHuadaoInfoDO.setId(list.get(0).getId());
            loanHuadaoInfoService.update(loanHuadaoInfoDO);
        } else {
            loanHuadaoInfoService.save(loanHuadaoInfoDO);
        }

        if ("200".equals(jsonObject.get("CODE").toString())) {
            model.addAttribute("CODE", "查询成功");
        } else {
            model.addAttribute("CODE", "查询失败");
        }
        model.addAttribute("PHONE", jsonObject.get("PHONE"));
        model.addAttribute("PROVINCE", jsonObject.get("PROVINCE"));
        model.addAttribute("CITY", jsonObject.get("CITY"));

        cn.hutool.json.JSONArray results = (cn.hutool.json.JSONArray) jsonObject.get("RESULTS");
        for (int i = 0; i < results.size(); i++) {
            //System.out.println(results.get(i));
            cn.hutool.json.JSONObject jsonObject1 = JSONUtil.parseObj(results.get(i).toString());

            if ("EMR002".equals(jsonObject1.get("TYPE"))) {
                cn.hutool.json.JSONArray jsonArray_EMR002 = JSONUtil.parseArray(jsonObject1.get("DATA").toString());
                for (int j = 0; j < jsonArray_EMR002.size(); j++) {
                    arrayList_EMR002.add((Map) jsonArray_EMR002.get(j));
                }
            }
            if ("EMR004".equals(jsonObject1.get("TYPE"))) {
                cn.hutool.json.JSONArray jsonArray_EMR004 = JSONUtil.parseArray(jsonObject1.get("DATA").toString());
                for (int j = 0; j < jsonArray_EMR004.size(); j++) {
                    arrayList_EMR004.add((Map) jsonArray_EMR004.get(j));
                }
            }
            if ("EMR007".equals(jsonObject1.get("TYPE"))) {
                cn.hutool.json.JSONArray jsonArray_EMR007 = JSONUtil.parseArray(jsonObject1.get("DATA").toString());
                for (int j = 0; j < jsonArray_EMR007.size(); j++) {
                    arrayList_EMR007.add((Map) jsonArray_EMR007.get(j));
                }
            }
            if ("EMR009".equals(jsonObject1.get("TYPE"))) {
                cn.hutool.json.JSONArray jsonArray_EMR009 = JSONUtil.parseArray(jsonObject1.get("DATA").toString());
                for (int j = 0; j < jsonArray_EMR009.size(); j++) {
                    arrayList_EMR009.add((Map) jsonArray_EMR009.get(j));
                }
            }
            if ("EMR012".equals(jsonObject1.get("TYPE"))) {
                cn.hutool.json.JSONArray jsonArray_EMR012 = JSONUtil.parseArray(jsonObject1.get("DATA").toString());
                for (int j = 0; j < jsonArray_EMR012.size(); j++) {
                    arrayList_EMR012.add((Map) jsonArray_EMR012.get(j));
                }
            }
            if ("EMR013".equals(jsonObject1.get("TYPE"))) {
                cn.hutool.json.JSONArray jsonArray_EMR013 = JSONUtil.parseArray(jsonObject1.get("DATA").toString());
                for (int j = 0; j < jsonArray_EMR013.size(); j++) {
                    arrayList_EMR013.add((Map) jsonArray_EMR013.get(j));
                }
            }
        }

        model.addAttribute("arrayList_EMR002", arrayList_EMR002);
        model.addAttribute("arrayList_EMR004", arrayList_EMR004);
        model.addAttribute("arrayList_EMR007", arrayList_EMR007);
        model.addAttribute("arrayList_EMR009", arrayList_EMR009);
        model.addAttribute("arrayList_EMR012", arrayList_EMR012);
        model.addAttribute("arrayList_EMR013", arrayList_EMR013);

        model.addAttribute("user", user);

        return "system/loanHuadaoInfo/huaDaoInfo";
    }


}
